package algorithm_optimal_solution.StackAndQueue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Stack;

public class TwoStackFromQueue {
    public static Stack<String> stackOne=new Stack<>();
    public static Stack<String> stackTwo=new Stack<>();

    public void oneToTwo(){
        if(stackTwo.isEmpty()){
            while(!stackOne.isEmpty()){
                stackTwo.push(stackOne.pop());
            }
        }
    }

    public void add(String word){
        stackOne.push(word);
        oneToTwo();
        /*stackTwo.clear();
        stackTwo.addAll(stackOne);
        Collections.reverse(stackTwo);*/
    }

    public void poll(){
        if(stackOne.isEmpty() || stackTwo.isEmpty()){
            throw new RuntimeException("stack中没有数据");
        }
        oneToTwo();
        stackTwo.pop();
        /*stackOne.clear();
        stackOne.addAll(stackTwo);
        Collections.reverse(stackOne);*/
    }

    public String peek(){
        if(stackOne.isEmpty() || stackTwo.isEmpty()){
            throw new RuntimeException("stack中没有数据");
        }
        oneToTwo();
        return stackTwo.peek();
    }

    public static void main(String[] args)throws IOException {
        TwoStackFromQueue main=new TwoStackFromQueue();
        BufferedReader scanner=new BufferedReader(new InputStreamReader(System.in));
        int len=Integer.parseInt(scanner.readLine());
        for(int i=0;i<len;i++){
            String value=scanner.readLine();
            if(value.equals("poll")){
                main.poll();
            }else if(value.equals("peek")){
                System.out.println(main.peek());
            }else{
                String[] result=value.split(" ");
                main.add(result[1]);
            }
        }
        scanner.close();
    }
}
